package com.galgram.project.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import com.galgram.project.domain.FxActivity;
import com.galgram.project.service.IFxActivityService;
import com.galgram.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxActivityUser;
import com.galgram.project.service.IFxActivityUserService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 个人活动Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/userActivity")
public class FxActivityUserController extends BaseController {
    @Autowired
    private IFxActivityUserService fxActivityUserService;
    @Autowired
    private IFxActivityService fxActivityService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询个人活动列表
     */
    @GetMapping("/getList")
    public TableDataInfo getList(FxActivityUser fxActivityUser) {
        startPage();
        List<FxActivity> list = fxActivityUserService.getUserActivityList(fxActivityUser.getUserId());
        return getDataTable(list);
    }

    /**
     * 查询活动报名状态
     */
    @GetMapping("/isApply/{userId}/{activityId}")
    public boolean getApplyState(@PathVariable("userId") Long userId, @PathVariable("activityId") Long activityId) {
        FxActivityUser fxActivityUser = new FxActivityUser();
        fxActivityUser.setUserId(userId);
        fxActivityUser.setActivityId(activityId);
        List<FxActivityUser> list = fxActivityUserService.selectFxActivityUserList(fxActivityUser);
        return list.size() == 0 ? false : true;
    }

    /**
     * 查询个人活动报名详情
     */
    @GetMapping("/getDetail")
    public TableDataInfo getUserActivityDetail(FxActivityUser fxActivityUser) {
        startPage();
        List<FxActivityUser> list = fxActivityUserService.selectFxActivityUserList(fxActivityUser);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setActivityName(fxActivityService.getById(list.get(i).getActivityId()).getTitle());
            list.get(i).setNickName(userService.selectUserById(list.get(i).getUserId()).getNickName());
        }
        return getDataTable(list);
    }

    /**
     * 查询个人活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxActivityUser fxActivityUser) {
        startPage();
        List<FxActivityUser> list = fxActivityUserService.selectFxActivityUserList(fxActivityUser);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setActivityName(fxActivityService.getById(list.get(i).getActivityId()).getTitle());
            list.get(i).setNickName(userService.selectUserById(list.get(i).getUserId()).getNickName());
        }
        return getDataTable(list);
    }

    /**
     * 获取个人活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxActivityUserService.getById(id));
    }

    /**
     * 报名活动
     */
    @PostMapping("/applyActivity")
    public AjaxResult apply(@RequestBody FxActivityUser fxActivityUser) {
        fxActivityUser.setPayType("微信支付");
        fxActivityUser.setApplyTime(LocalDateTime.now());
        fxActivityUserService.save(fxActivityUser);
        return success("添加成功");
    }

    /**
     * 新增个人活动
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:add')")
    @Log(title = "个人活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxActivityUser fxActivityUser) {
        fxActivityUserService.save(fxActivityUser);
        return success("添加成功");
    }

    /**
     * 修改个人活动
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:edit')")
    @Log(title = "个人活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxActivityUser fxActivityUser) {
        fxActivityUserService.updateById(fxActivityUser);
        return success("修改成功");
    }

    /**
     * 删除个人活动
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:remove')")
    @Log(title = "个人活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxActivityUserService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出个人活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:export')")
    @Log(title = "个人活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxActivityUser fxActivityUser) {
        List<FxActivityUser> list = fxActivityUserService.selectFxActivityUserList(fxActivityUser);
        ExcelUtil<FxActivityUser> util = new ExcelUtil<FxActivityUser>(FxActivityUser.class);
        util.exportExcel(response, list, "个人活动数据");
    }

    /**
     * 导入个人活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:import')")
    @Log(title = "个人活动", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxActivityUser> util = new ExcelUtil<FxActivityUser>(FxActivityUser.class);
        List<FxActivityUser> userList = util.importExcel(file.getInputStream());
        fxActivityUserService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:userActivity:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxActivityUser> list = new ArrayList<FxActivityUser>();
        ExcelUtil<FxActivityUser> util = new ExcelUtil<FxActivityUser>(FxActivityUser.class);
        util.exportExcel(response, list, "个人活动数据");
    }
}
